package com.water.camera.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.water.camera.R;
import com.water.camera.activity.BatchPhotoAct;
import com.water.camera.bean.MediaData;
import com.water.camera.databinding.ItemAlbumBinding;
import com.water.camera.databinding.ItemAlbumHeadBinding;
import com.water.camera.util.Utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class BatchAdapter extends RecyclerView.Adapter {

    ArrayList list;
    Set<Long> set = new HashSet<>();
    int count = 0;
    BatchPhotoAct act;

    public BatchAdapter(BatchPhotoAct act) {
        this.act = act;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            ItemAlbumHeadBinding binding = ItemAlbumHeadBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new HeadHolder(binding);
        } else {
            ItemAlbumBinding binding = ItemAlbumBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ImgHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadHolder) {
            ((HeadHolder) holder).bind(position);
        } else if (holder instanceof ImgHolder) {
            ((ImgHolder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position) instanceof MediaData) {
            return 1;
        }
        return 0;
    }

    public void setData(ArrayList list, int page) {
        if (page == 0) {
            this.list = list;
            notifyDataSetChanged();
        } else {
            if (list == null || list.isEmpty()) {
                return;
            }
            MediaData data = (MediaData) this.list.get(this.list.size() - 1);
            if (Utils.isSameDate(data.getAddTime(), ((MediaData) list.get(1)).getAddTime())) {
                list.remove(0);
            }
            this.list.addAll(list);
            notifyItemRangeInserted(this.list.size(), list.size());
        }
    }

    class HeadHolder extends RecyclerView.ViewHolder {

        ItemAlbumHeadBinding binding;

        public HeadHolder(ItemAlbumHeadBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            long time = (Long) list.get(p);
            binding.tvTitle.setText(Utils.getDay5(time));
            if (set.contains(time)) {
                binding.tvAllSelect.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_more_select, 0, 0, 0);
            } else {
                binding.tvAllSelect.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_more_unselect, 0, 0, 0);
            }
            binding.tvAllSelect.setOnClickListener(v -> {
                boolean bo = set.contains(time);
                if (bo) {
                    set.remove(time);
                } else {
                    set.add(time);
                }
                int num = 1;
                for (int i = getBindingAdapterPosition() + 1; i < getItemCount(); i++) {
                    if (list.get(i) instanceof MediaData) {
                        ++num;
                        ((MediaData) list.get(i)).setSetSelected(!bo);
                    } else {
                        break;
                    }
                }
                if (bo) {
                    count -= (num - 1);
                } else {
                    count += (num - 1);
                }
                act.selectChange(count);
                notifyItemRangeChanged(getBindingAdapterPosition(), num);
            });
        }
    }

    class ImgHolder extends RecyclerView.ViewHolder {
        ItemAlbumBinding binding;

        public ImgHolder(ItemAlbumBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            MediaData data = (MediaData) list.get(p);
            if (data.isSetSelected()) {
                binding.ivSelect.setImageResource(R.drawable.icon_album_select);
            } else {
                binding.ivSelect.setImageResource(R.drawable.icon_album_unselect);
            }
            Glide.with(itemView.getContext())
                    .setDefaultRequestOptions(RequestOptions.centerCropTransform())
                    .load(data.getPath())
//                    .override(data.getWidth(), data.getHeight())
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(binding.ivCover);
            itemView.setOnClickListener(v -> {
                boolean bo = data.isSetSelected();
                data.setSetSelected(!bo);
                if (data.isSetSelected()) {
                    ++count;
                    binding.ivSelect.setImageResource(R.drawable.icon_album_select);
                    int tmp = getBindingAdapterPosition();
                    boolean all = true;
                    int head = -1;
                    for (int i = tmp - 1; i >= 0; i--) {
                        if (list.get(i) instanceof MediaData) {
                            if (!((MediaData) list.get(i)).isSetSelected()) {
                                all = false;
                                break;
                            }
                        } else {
                            head = i;
                            break;
                        }
                    }
                    for (int i = tmp + 1; i < getItemCount(); i++) {
                        if (list.get(i) instanceof MediaData) {
                            if (!((MediaData) list.get(i)).isSetSelected()) {
                                all = false;
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                    if (all && head >= 0) {
                        long time = (Long) list.get(head);
                        if (!set.contains(time) && all) {
                            set.add(time);
                            notifyItemChanged(head);
                        }
                    }
                } else {
                    --count;
                    binding.ivSelect.setImageResource(R.drawable.icon_album_unselect);
                    int tmp = getBindingAdapterPosition();
                    for (int i = tmp - 1; i >= 0; i--) {
                        if (list.get(i) instanceof MediaData == false) {
                            long time = (Long) list.get(i);
                            if (set.contains(time)) {
                                set.remove(time);
                                notifyItemChanged(i);
                            }
                            break;
                        }
                    }
                }
                act.selectChange(count);
            });
        }
    }

    public ArrayList<MediaData> getSelect() {
        ArrayList tmp = new ArrayList();
        for (Object obj : list) {
            if (obj instanceof MediaData && ((MediaData) obj).isSetSelected()) {
                tmp.add(obj);
            }
        }
        return tmp;
    }
}
